﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EvoWars.Stuffs.TankStuff;
using Microsoft.Xna.Framework;

namespace EvoWars2.Stuffs.AI
{
    class WandererAI : IArtificialIntelligence
    {

        public Tank Parent { get; set; }
        public Random Rand { get; set; }

        public void Init()
        {
            //pick a random direction
            Vector2 direction = new Vector2((float)(Rand.NextDouble() * 2) - 1, (float)(Rand.NextDouble() * 2) - 1);
            direction.Normalize();
            direction = direction * (float)Parent.Attributes.MaxSpeed;
            Parent.CurrentVelocity = direction;
        }



        private Vector2 destinationVector;
        private TimeSpan totalElapsedTime;
        private int lastCheckCount;

        public void DoUpdate(TimeSpan elapsedTime)
        {
            bool bounce;

            bounce = !Parent.Sensor.CanMoveByVector(Parent.CurrentVelocity);

            if (bounce)
            {
                Parent.CurrentVelocity = Parent.CurrentVelocity * -1f;
            }

            totalElapsedTime += elapsedTime;
            //only check once a second
            if (totalElapsedTime.Seconds > lastCheckCount)
            {
                lastCheckCount = totalElapsedTime.Seconds;

                //10% chance to change vectors
                if (Rand.NextDouble() > .1)
                {
                    destinationVector = new Vector2((float)(Rand.NextDouble() * 2) - 1, (float)(Rand.NextDouble() * 2) - 1);
                    destinationVector.Normalize();

                }
            }

            //slowly head toward destinationVector
            if (destinationVector != Parent.CurrentVelocity)
            {
                Vector2 tempVector = Parent.CurrentVelocity + (destinationVector * .5f);
                tempVector.Normalize();
                tempVector = tempVector * (float)Parent.Attributes.MaxSpeed;

                Parent.CurrentVelocity = tempVector;
            }

        }
    }
}
